package com.sq.partner.manager.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.DictCache;
import com.sq.partner.manager.dao.TradePlanFeeDao;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.mybatis.model.TradePlanFee;
import com.sq.partner.manager.service.TradPlanFeeService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

@Service
public class TradPlanFeeServiceImpl implements TradPlanFeeService {

	@Resource
	TradePlanFeeDao tradePlanFeeDao;
	@Override
	public Page<TradePlanFee> queryPagePlanFee(Page<TradePlanFee> page) throws Exception {
	    page=tradePlanFeeDao.queryPagePlanFee(page);
	   List<TradePlanFee> list= page.getData();
	   for (int i = 0; i < list.size(); i++) {
		Map<String,Object> map=(Map) list.get(i);
		managerVhl(map, i);
	   }
		return page;
	}
	Map<String, Object> map1=new HashMap<String, Object>();
	/**车险车辆标志处理**/
	private Map managerVhl(Map<String, Object> map,Integer  mapCount){
		String vhl="";
		if( map.get("VHL_TYP")==null){
			map.put("VHL_NLIFE_CAR", "");
			return map;
		}
		vhl=(String) map.get("VHL_TYP");
		/**标志每6位需要截取**/
		if(StrUtil.isNotEmpty(vhl)){
			Integer vhlLength=vhl.length();
			StringBuffer changeType=new StringBuffer("");
			for (int i = 0; i < vhlLength/6; i++) {
				String car_type=vhl.substring(i*6,(i+1)*6);
				if(i==0){
					changeType.append(car_type);
				}else{
					changeType.append(","+car_type);
				}
				if(mapCount==0){
					List<DictDto> vhlNlifeCar=DictCache.getDict(Constants.DICT_VHL_NLIFE_CAR);
					for (int j = 0; j < vhlNlifeCar.size(); j++) {
						map1.put(vhlNlifeCar.get(j).getCode(), vhlNlifeCar.get(j));
					}
				}
				//core_prod_code
				Object carChange=map1.get(car_type);
				if(carChange!=null){
					DictDto dictDto=(DictDto) carChange;
					String coreProd=(String) map.get("CORE_PROD_CODE");
					if("1949".equals(coreProd)){
						map.put("VHL_NLIFE_CAR", dictDto.getOtherName());
					}else if("1933".equals(coreProd)){
						map.put("VHL_NLIFE_CAR", dictDto.getCode2Name());
					}else if("1948".equals(coreProd)){
						map.put("VHL_NLIFE_CAR", "");
					}
				}
				map.put("VHL_TYP",changeType.toString());
			}
		}
		return map;
	}
	@Override
	public void deletePlanFeeByAgentId(String agentInfoId) throws Exception {
		tradePlanFeeDao.deletePlanFeeByAgentId(agentInfoId);
	}
	@Override
	public boolean addTradPlanFee(List<TradePlanFee> list) throws Exception {
		return tradePlanFeeDao.addTradPlanFee(list);
	}
	
}
